import { ref, computed } from 'vue'
import { defineStore } from 'pinia'
import type { NginxConfig } from '@/types'
import { ElMessage } from 'element-plus'

export const useNginxStore = defineStore('nginx', () => {
  const configs = ref<NginxConfig[]>([])
  const currentConfig = ref<NginxConfig | null>(null)
  const loading = ref(false)
  const error = ref<string | null>(null)


  // 获取单个配置
  const fetchConfig = async (id: number) => {
    loading.value = true
    error.value = null
    try {
      const config = configs.value.find(c => c.id === id)
      if (config) {
        currentConfig.value = config
      } else {
        throw new Error('配置不存在')
      }
    } catch (err) {
      error.value = err instanceof Error ? err.message : '获取配置失败'
      ElMessage.error(error.value)
    } finally {
      loading.value = false
    }
  }

  // 创建配置
  const createConfig = async (config: Omit<NginxConfig, 'id' | 'isActive' | 'createdAt' | 'updatedAt'>) => {
    loading.value = true
    error.value = null
    try {
      // 模拟 API 调用
      const newConfig: NginxConfig = {
        id: configs.value.length + 1,
        ...config,
        isActive: false,
        createdAt: new Date().toISOString(),
        updatedAt: new Date().toISOString()
      }
      configs.value.push(newConfig)
      currentConfig.value = newConfig
      ElMessage.success('创建配置成功')
    } catch (err) {
      error.value = err instanceof Error ? err.message : '创建配置失败'
      ElMessage.error(error.value)
      throw err
    } finally {
      loading.value = false
    }
  }

  // 更新配置
  const updateConfig = async (id: number, content: string) => {
    loading.value = true
    error.value = null
    try {
      const config = configs.value.find(c => c.id === id)
      if (config) {
        config.content = content
        config.updatedAt = new Date().toISOString()
        ElMessage.success('更新配置成功')
      } else {
        throw new Error('配置不存在')
      }
    } catch (err) {
      error.value = err instanceof Error ? err.message : '更新配置失败'
      ElMessage.error(error.value)
      throw err
    } finally {
      loading.value = false
    }
  }

  // 删除配置
  const deleteConfig = async (id: number) => {
    loading.value = true
    error.value = null
    try {
      const index = configs.value.findIndex(c => c.id === id)
      if (index !== -1) {
        configs.value.splice(index, 1)
        if (currentConfig.value?.id === id) {
          currentConfig.value = configs.value[0] || null
        }
        ElMessage.success('删除配置成功')
      } else {
        throw new Error('配置不存在')
      }
    } catch (err) {
      error.value = err instanceof Error ? err.message : '删除配置失败'
      ElMessage.error(error.value)
      throw err
    } finally {
      loading.value = false
    }
  }

  // 测试配置
  const testConfig = async (id: number) => {
    loading.value = true
    error.value = null
    try {
      // 模拟 API 调用
      await new Promise(resolve => setTimeout(resolve, 1000))
      ElMessage.success('配置测试通过')
    } catch (err) {
      error.value = err instanceof Error ? err.message : '测试配置失败'
      ElMessage.error(error.value)
      throw err
    } finally {
      loading.value = false
    }
  }

  // 应用配置
  const applyConfig = async (id: number) => {
    loading.value = true
    error.value = null
    try {
      const config = configs.value.find(c => c.id === id)
      if (config) {
        config.isActive = true
        config.updatedAt = new Date().toISOString()
        // 将其他配置设置为非激活
        configs.value.forEach(c => {
          if (c.id !== id) {
            c.isActive = false
          }
        })
        ElMessage.success('应用配置成功')
      } else {
        throw new Error('配置不存在')
      }
    } catch (err) {
      error.value = err instanceof Error ? err.message : '应用配置失败'
      ElMessage.error(error.value)
      throw err
    } finally {
      loading.value = false
    }
  }

  return {
    configs,
    currentConfig,
    loading,
    error,
    fetchConfig,
    createConfig,
    updateConfig,
    deleteConfig,
    testConfig,
    applyConfig
  }
}) 